#include"MyPlayer.h"

void ShowMetaData(const AVDictionary *m){
	 AVDictionaryEntry *tag = NULL;
	 printf("ShowMetaDataStart********\n");
	 while((tag = av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX))){
		printf("%s=%s\n", tag->key, tag->value);
	 }
	 printf("ShowMetaDataEnd********\n\n");
}
//AVMEDIA_TYPE_VIDEO
//AVMEDIA_TYPE_AUDIO
int GetStreamIdxByType(AVFormatContext*fmt_ctx,enum AVMediaType type){
	int idx=-1,i=0;
	for(i=0;i<fmt_ctx->nb_streams;i++){
		if (fmt_ctx->streams[i]->codec->codec_type == type){
			idx=i;
			break;
		}
	}
	return idx;
}
//Try Find Video Info
int FindVideoCodec(AVFormatContext*fmt_ctx){
	int video_stream_idx=GetStreamIdxByType(fmt_ctx,AVMEDIA_TYPE_VIDEO);
	if(video_stream_idx >= 0) {
		AVStream*video_stream = fmt_ctx->streams[video_stream_idx];
		AVCodecContext*video_dec_ctx = video_stream->codec;
		printf("Find Video Info:\n");
		printf("idx:%d dec:0x%08x %s\n",video_stream_idx,video_dec_ctx->codec_id,avcodec_get_name(video_dec_ctx->codec_id));
        //AV_PIX_FMT_YUV420P
		printf("pix_fmt:%s Size: %d x %d\n",
		av_get_pix_fmt_name(video_dec_ctx->pix_fmt),
		video_dec_ctx->width, video_dec_ctx->height);
        //video_dec_ctx->pix_fmt=AV_PIX_FMT_YUV420P;
    }else{
		printf("Can't Find Video Info\n");
	}
	printf("\n");
	return video_stream_idx;
}
//Tay Find Audio Info
int FindAudioCodec(AVFormatContext*fmt_ctx){
	int audio_stream_idx=audio_stream_idx=GetStreamIdxByType(fmt_ctx,AVMEDIA_TYPE_AUDIO);
	if(audio_stream_idx>=0){
		AVStream *audio_stream = fmt_ctx->streams[audio_stream_idx];
		AVCodecContext *audio_dec_ctx = audio_stream->codec;
		printf("Find Audio Info:\n");    
		printf("idx:%d dec:0x%08x %s\n",audio_stream_idx,audio_dec_ctx->codec_id,avcodec_get_name(audio_dec_ctx->codec_id));
        //AV_SAMPLE_FMT_FLTP
		printf("sample_fmt:%s sample_rate:%d channels:%d frame_size:%d\n", 
		av_get_sample_fmt_name(audio_dec_ctx->sample_fmt),
		audio_dec_ctx->sample_rate,audio_dec_ctx->channels,audio_dec_ctx->frame_size);
        //audio_dec_ctx->sample_fmt=AV_SAMPLE_FMT_S16;
	}else{
		printf("Can't Find Audio\n");
	}
	printf("\n");
	return audio_stream_idx;
}

void ShowAllStreams(AVFormatContext *fmt_ctx){
    int i=0;
    for(i=0;i<fmt_ctx->nb_streams;i++){
       printf("StreamIndex:%d Type:%d CodecId:0x%08x %s\n",i,
       fmt_ctx->streams[i]->codec->codec_type,
       fmt_ctx->streams[i]->codec->codec_id,
       avcodec_get_name(fmt_ctx->streams[i]->codec->codec_id));
    }
}
void ShowAllProgram(AVFormatContext *fmt_ctx){
    int i=0;
    for(i=0;i<fmt_ctx->nb_programs;i++){
        int j=0;
        printf("ProgramsIndex:%d Streams:%u\n",i,fmt_ctx->programs[i]->nb_stream_indexes);    
        for(j=0;j<fmt_ctx->programs[i]->nb_stream_indexes;j++){
            printf("ProgramsIndex:%d StreamsIndex:%u\n",fmt_ctx->programs[i]->id,fmt_ctx->programs[i]->stream_index[j]);
        }
    }
}